use server::{tcp_server, websocket_server};
use server::config::app_config::CONFIG;
use server::config::log_config::config_tracing_log;

pub mod ffi;

pub mod config;
pub mod keyboard;

pub mod tcp;
pub mod websocket;

pub fn main() -> anyhow::Result<()> {
    let runtime = tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()?;

    let _work_guard = config_tracing_log()?;

    if std::env::var_os("RUST_LOG").is_none() {
        unsafe {
            std::env::set_var(
                "RUST_LOG",
                "sqlx=debug,salvo=debug",
            );
        }
    }
    let server_config = &CONFIG.server;
    let tcp_addr = &server_config.tcp;
    let ws_addr = &server_config.ws;

    log::debug!("program running on pid ={}", std::process::id());
    runtime.spawn(websocket_server(ws_addr));
    runtime.block_on(tcp_server(tcp_addr))?;

    Ok(())
}
